<template>
  <div class="container" :style="{ backgroundImage: bg }">
    <!--对话框 start-->
    <div>
      <!--添加-->
      <el-dialog draggable destroy-on-close v-model="dialogAddVisible" title="添加" width="35%" center>
        <el-form inline :model="addFormInfo" label-width="150px">
          <el-form-item label="名称">
            <el-input v-model="addFormInfo.permissionName"></el-input>
          </el-form-item>
          <el-form-item label="详情">
            <el-input v-model="addFormInfo.permissionDesc"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogAddVisible = false">取消</el-button>
                <el-button type="primary" @click="sureHandler">确定</el-button>
            </span>
        </template>
      </el-dialog>
      <!--编辑-->
      <el-dialog
          draggable
          destroy-on-close
          v-model="dialogEditorVisible"
          title="编辑"
          width="35%"
          center>
        <el-form inline :model="editorFormInfo" label-width="150px">
          <el-form-item label="名称">
            <el-input v-model="editorFormInfo.permissionName"></el-input>
          </el-form-item>
          <el-form-item label="详情">
            <el-input v-model="editorFormInfo.permissionDesc"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogEditorVisible = false">取消</el-button>
                <el-button type="primary" @click="sureEditorHandler">确定</el-button>
            </span>
        </template>
      </el-dialog>
    </div>
    <!--对话框 end-->

    <!--顶部 start-->
    <div class="header" v-if="headerShow">
      <div class="search">
        <el-input class="input" size="large" @keyup.enter="searchHandle" v-model="searchInfo"
                  placeholder="请输入关键字"></el-input>
        <el-button icon="Search" size="large" @click="searchHandle" color="#3c8dbc">搜索</el-button>
      </div>
    </div>
    <!--顶部 end-->

    <!--表格 start-->
    <div class="table-container">
      <div class="table-header">
        <div class="button-list">
          <el-button icon="DocumentAdd" type="success" @click="addHander">新增</el-button>
          <el-button icon="DocumentAdd" type="primary">导入</el-button>
          <el-button icon="DocumentAdd" type="warning">导出</el-button>
        </div>
        <div class="icon-list">
          <el-button icon="search" color="#3c8dbc" @click="headerShow?headerShow=false:headerShow=true"></el-button>
          <el-button icon="RefreshRight" color="#269846" @click="reload"></el-button>
          <el-button icon="ArrowLeftBold" color="#133c9a" @click="onClickLeft"></el-button>
        </div>
      </div>
      <el-table class="table" :data="dataList.list" highlight-current-row height="400px">
        <el-table-column label="名称" prop="permissionName" align="center"></el-table-column>
        <el-table-column label="详情" prop="permissionDesc" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="220">
          <template #default="scope">
            <el-button size="small" icon="edit" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button size="small" icon="delete" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          layout="prev,pager,next,jumper"
          :default-page-size="defaultPageSize"
          :total="totalData"
          @current-change="currentChangeHaddler"></el-pagination>
    </div>
    <!--表格 end-->

    <!--底部 start-->
    <div class="bottom">
      <span>© 2024 YueMu Copyrigh</span>
    </div>
    <!--底部 end-->
  </div>
</template>

<script setup>
import sysAxios from "@/api/sys.js"
import {onMounted, reactive, ref} from "vue";
import {Apple, Cherry, Grape, Moon, Orange, Pear, Star, Sunny, Watermelon,} from '@element-plus/icons-vue'
//顶部显示
const headerShow = ref(true)
/**
 * 返回
 */
const onClickLeft = () => {
  history.back();
}
/**
 * 刷新页面
 */
const reload = () => {
  location.reload();
}
//初始化分页查询数据
const dataList = reactive({
  list: []
})
//初始化总条数
const totalData = ref(0)
//当前页
const currentPage = ref(1)
//初始化分页显示条数
const defaultPageSize = ref(10)
//搜索初始化状态
const searchInfo = ref("")
//添加添加对话框控制器
const dialogAddVisible = ref(false)
//初始化添加对话框状态
const addFormInfo = reactive({
  permissionName: "",
  permissionDesc: "",
})
//编辑对话框控制器
const dialogEditorVisible = ref(false)
//初始化编辑对话框状态
const editorFormInfo = reactive({
  permissionId: '',
  permissionName: "",
  permissionDesc: "",
})
/**
 * 网路请求：分页查询
 */
const http = () => {
  sysAxios.permission_search({
    search: searchInfo.value,
    page: currentPage.value,
    size: defaultPageSize.value
  }).then(res => {
    if (res.data.code == 200) {
      dataList.list = res.data.data.records
      totalData.value = res.data.data.total
    } else {
      ElMessage.error(res.data.message)
    }

  })
}
onMounted(() => {
  http()
})
/**
 * 分页
 */
const currentChangeHaddler = (nowPage) => {
  currentPage.value = nowPage
  http()
}
/**
 * 搜索按钮
 */
const searchHandle = () => {
  http()
}
/**
 * 添加对话框弹出事件
 */
const addHander = () => {
  dialogAddVisible.value = true
}
/**
 * 添加对话框 确定事件
 */
const sureHandler = () => {
  sysAxios.permission_add({
    permissionName: addFormInfo.permissionName,
    permissionDesc: addFormInfo.permissionDesc,
  }).then(res => {
    if (res.data.code == 200) {
      dialogAddVisible.value = false
      http()
    } else {
      ElMessage.error(res.data.message)
    }
  })
}
/**
 * 编辑对话框 弹出事件
 */
const handleEdit = (index, row) => {
  dialogEditorVisible.value = true
  sysAxios.permission_findById({
    permissionId: row.permissionId
  }).then(res => {
    if (res.data.code == 200) {
      editorFormInfo.permissionId = res.data.data.permissionId;
      editorFormInfo.permissionName = res.data.data.permissionName;
      editorFormInfo.permissionDesc = res.data.data.permissionDesc;
    } else {
      ElMessage.error(res.data.data.message)
    }
  })

}
/**
 * 编辑对话框 确定事件
 */
const sureEditorHandler = () => {
  sysAxios.permission_update({
    permissionId: editorFormInfo.permissionId,
    permissionName: editorFormInfo.permissionName,
    permissionDesc: editorFormInfo.permissionDesc,
  }).then(res => {
    if (res.data.code == 200) {
      dialogEditorVisible.value = false
      http()
    } else {
      ElMessage.error(res.data.message)
    }
  })
}
/**
 * 删除
 */
const handleDelete = (index, row) => {
  ElMessageBox.confirm(
      '确定删除么',
      '删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    sysAxios.permission_delete({
      permissionId: row.permissionId
    }).then(res => {
      if (res.data.code == 200) {
        ElMessage({
          type: 'success',
          message: "删除成功！！！",
        })
        http()
      } else {
        ElMessage({
          type: 'error',
          message: res.data.message,
        })
      }
    })
  }).catch(error => {
    ElMessage({
      type: 'info',
      message: "取消删除",
    })
  })
}
</script>
<style scoped lang="scss">
.container {
  padding-top: 20px;
  background-size: cover;
}

.header {
  background-color: #ffffff;
  margin-left: 10px;
  margin-right: 10px;
  margin-bottom: 15px;
  padding: 12px;
  border-radius: 10px;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  display: flex;

  .search {
    width: 100%;

    .input {
      width: 45%;
    }
  }

}

.table-container {
  background-color: #ffffff;
  margin: 10px;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);

  .table-header {
    display: flex;

    .button-list {
      color: #ffffff;
    }

    .icon-list {
      position: fixed;
      right: 30px;
    }
  }

  .table {
    margin: 10px;
    width: 97%;
  }
}

.bottom {
  position: fixed;
  bottom: 0;
  height: 20px;
  background-color: #ffffff;
  width: 100%;
  padding: 10px;

  span {
    position: fixed;
    right: 20px;
    cursor: pointer;
  }
}

</style>
